//
//  detailsViewController.m
//  随便点
//
//  Created by MWJ on 15/3/18.
//  Copyright (c) 2015年 qfsoft. All rights reserved.
//

#import "detailsViewController.h"
#import "AppDelegate.h"
#import "MainTableViewCell.h"
#import "xiangqing.h"
#define Kxianshi @"http://www.51anys.com:8080/waimaiwang/order/details"
@interface detailsViewController (){
    AppDelegate *app;
    NSMutableArray *array;
    NSMutableArray *xqarray;
    NSDictionary *dic;
    NSDictionary *dic2;
    NSDictionary *dic3;
    NSMutableArray *array3;
    UILabel *lab1;
    UILabel *lab2;
    UILabel *lab3;
    UILabel *lab4;
    UILabel *lab5;
    UILabel *lab6;
    UILabel *lab7;
    UILabel *lab8;
    UILabel *lab9;
    UILabel *lab10;
    UILabel *lab11;
    UILabel *lab12;
    UILabel *lab13;
    UILabel *lab14;
    UILabel *lab15;
    UILabel *lab16;
    UILabel *lab17;
    UILabel *lab18;
    UILabel *lab19;
    UILabel *lab20;
    UILabel *lab21;
    UILabel *lab22;
    UILabel *lab23;
}

@end

@implementation detailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = item;
    [self loading];
    app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    [self Request];
    //隐藏tableview分割线
    self.tableview.separatorStyle=NO;
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)Request{
    NSURL *url=[NSURL URLWithString:Kxianshi];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *postStr=[NSString stringWithFormat:@"orderId=%@",app.dingdanid];
    [request setHTTPMethod:@"post"];
    NSData *data=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError){
        dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData:) withObject:array waitUntilDone:YES];
        dic2 = dic[@"order"];
        array3 = dic[@"goodsList"];
        NSString *str = @"共";
        NSString *str1 = dic2[@"count"];
        NSString *str2 = @"件";
        NSString *str3 = @"配送费:";
        NSString *str4 = dic2[@"sendCost"];
        NSString *str5 = @"元";
        NSString *str6 = @"总金额:";
        NSString *str7 = dic2[@"sum"];
        for (NSDictionary *obj in array3){
            lab14.text = obj[@"goodsName"];
        }
        lab2.text = [NSString stringWithFormat:@"%@",dic2[@"id"]];
        lab8.text = [NSString stringWithFormat:@"%@",dic2[@"username"] ];
        lab9.text = [NSString stringWithFormat:@"%@",dic2[@"phone"] ];
        //时间戳转换成时间
        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"yyyy-MM-dd HH:MM:ss"];
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[dic2[@"startTime"] doubleValue]/1000];
        NSString *timeSp = [formatter stringFromDate:confromTimesp];
        lab10.text=timeSp;
        
        lab11.text = [NSString stringWithFormat:@"%@",dic2[@"address"]];
        lab12.text = [NSString stringWithFormat:@"%@",dic2[@"state"]];
        lab15.text = [NSString stringWithFormat:@"%@%@%@",str,str1,str2];
        lab16.text = [NSString stringWithFormat:@"%@%@%@",str3,str4,str5];
        lab17.text = [NSString stringWithFormat:@"%@%@%@",str6,str7,str5];
        [self.tableview reloadData];

    }];
}
-(void)refreshData:(NSArray *)sender{
}
//这个方法告诉表格有几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 50;
    }
    if (indexPath.row==1) {
        return 135;
    }
    if (indexPath.row==2) {
        return 85;
    }
    if (indexPath.row==3) {
        return 40;
    }
    if (indexPath.row==4) {
        return 25;
    }else{
        return 0;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier=@"cell";
    MainTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil){
        cell=[[MainTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    switch (indexPath.row) {
        case 0:
            [cell addSubview:lab1];
            [cell addSubview:lab2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            break;
        case 1:
            [cell addSubview:lab3];
            [cell addSubview:lab4];
            [cell addSubview:lab5];
            [cell addSubview:lab6];
            [cell addSubview:lab7];
            [cell addSubview:lab8];
            [cell addSubview:lab9];
            [cell addSubview:lab10];
            [cell addSubview:lab11];
            [cell addSubview:lab12];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            break;
        case 2:
            [cell addSubview:lab13];
            [cell addSubview:lab14];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            break;
        case 3:
            [cell addSubview:lab15];
            [cell addSubview:lab16];
            [cell addSubview:lab17];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    return cell;
}
-(void)loading{
    UILabel *label1=[[UILabel alloc]init];
    CGFloat label1X=10;
    CGFloat label1Y=5;
    CGFloat label1W=100;
    CGFloat label1H=40;
    label1.frame=CGRectMake(label1X, label1Y, label1W, label1H);
    label1.text=@"订单号：";
    label1.font=[UIFont systemFontOfSize:25];
    label1.textColor=[UIColor blackColor];
    label1.backgroundColor=[UIColor whiteColor];
    lab1 = label1;
    
    UILabel *label2=[[UILabel alloc]init];
    CGFloat label2X=10+label1W;
    CGFloat label2Y=5;
    CGFloat label2W=200;
    CGFloat label2H=40;
    label2.frame=CGRectMake(label2X, label2Y, label2W, label2H);
    label2.text=@"123456789";
    label2.font=[UIFont systemFontOfSize:20];
    label2.textColor=[UIColor redColor];
    label2.backgroundColor=[UIColor whiteColor];
    lab2 = label2;
    
    UILabel *label3=[[UILabel alloc]init];
    CGFloat label3X=15;
    CGFloat label3Y=5;
    CGFloat label3W=80;
    CGFloat label3H=25;
    label3.frame=CGRectMake(label3X, label3Y, label3W, label3H);
    label3.text=@"姓       名：";
    label3.font=[UIFont systemFontOfSize:15];
    label3.textColor=[UIColor blackColor];
    label3.backgroundColor=[UIColor whiteColor];
    lab3 = label3;
    
    UILabel *label4=[[UILabel alloc]init];
    CGFloat label4X=15;
    CGFloat label4Y=30;
    CGFloat label4W=80;
    CGFloat label4H=25;
    label4.frame=CGRectMake(label4X, label4Y, label4W, label4H);
    label4.text=@"电       话：";
    label4.font=[UIFont systemFontOfSize:15];
    label4.textColor=[UIColor blackColor];
    label4.backgroundColor=[UIColor whiteColor];
    lab4 = label4;
    
    UILabel *label5=[[UILabel alloc]init];
    CGFloat label5X=15;
    CGFloat label5Y=55;
    CGFloat label5W=80;
    CGFloat label5H=25;
    label5.frame=CGRectMake(label5X, label5Y, label5W, label5H);
    label5.text=@"下单时间：";
    label5.font=[UIFont systemFontOfSize:15];
    label5.textColor=[UIColor blackColor];
    label5.backgroundColor=[UIColor whiteColor];
    lab5 = label5;
    
    UILabel *label6=[[UILabel alloc]init];
    CGFloat label6X=15;
    CGFloat label6Y=80;
    CGFloat label6W=80;
    CGFloat label6H=25;
    label6.frame=CGRectMake(label6X, label6Y, label6W, label6H);
    label6.text=@"收货地址：";
    label6.font=[UIFont systemFontOfSize:15];
    label6.textColor=[UIColor blackColor];
    label6.backgroundColor=[UIColor whiteColor];
    lab6 = label6;
    
    UILabel *label7=[[UILabel alloc]init];
    CGFloat label7X=15;
    CGFloat label7Y=105;
    CGFloat label7W=80;
    CGFloat label7H=25;
    label7.frame=CGRectMake(label7X, label7Y, label7W, label7H);
    label7.text=@"订单状态：";
    label7.font=[UIFont systemFontOfSize:15];
    label7.textColor=[UIColor blackColor];
    label7.backgroundColor=[UIColor whiteColor];
    lab7 = label7;
    
    UILabel *label8=[[UILabel alloc]init];
    CGFloat label8X=15+label3W;
    CGFloat label8Y=5;
    CGFloat label8W=150;
    CGFloat label8H=25;
    label8.frame=CGRectMake(label8X, label8Y, label8W, label8H);
    label8.text=@"";
    label8.font=[UIFont systemFontOfSize:15];
    label8.textColor=[UIColor blackColor];
    label8.backgroundColor=[UIColor whiteColor];
    lab8 = label8;
    
    UILabel *label9=[[UILabel alloc]init];
    CGFloat label9X=15+label3W;
    CGFloat label9Y=30;
    CGFloat label9W=150;
    CGFloat label9H=25;
    label9.frame=CGRectMake(label9X, label9Y, label9W, label9H);
    label9.text=@"";
    label9.font=[UIFont systemFontOfSize:15];
    label9.textColor=[UIColor blackColor];
    label9.backgroundColor=[UIColor whiteColor];
    lab9 = label9;
    
    UILabel *label10=[[UILabel alloc]init];
    CGFloat label10X=15+label3W;
    CGFloat label10Y=55;
    CGFloat label10W=150;
    CGFloat label10H=25;
    label10.frame=CGRectMake(label10X, label10Y, label10W, label10H);
    label10.text=@"";
    label10.font=[UIFont systemFontOfSize:15];
    label10.textColor=[UIColor blackColor];
    label10.backgroundColor=[UIColor whiteColor];
    lab10 = label10;
    
    UILabel *label11=[[UILabel alloc]init];
    CGFloat label11X=15+label3W;
    CGFloat label11Y=80;
    CGFloat label11W=150;
    CGFloat label11H=25;
    label11.frame=CGRectMake(label11X, label11Y, label11W, label11H);
    label11.text=@"";
    label11.font=[UIFont systemFontOfSize:15];
    label11.textColor=[UIColor blackColor];
    label11.backgroundColor=[UIColor whiteColor];
    lab11 = label11;
    
    UILabel *label12=[[UILabel alloc]init];
    CGFloat label12X=15+label3W;
    CGFloat label12Y=105;
    CGFloat label12W=150;
    CGFloat label12H=25;
    label12.frame=CGRectMake(label12X, label12Y, label12W, label12H);
    label12.text=@"";
    label12.font=[UIFont systemFontOfSize:15];
    label12.textColor=[UIColor blackColor];
    label12.backgroundColor=[UIColor whiteColor];
    lab12 = label12;
    
    UILabel *label13=[[UILabel alloc]init];
    CGFloat label13X=10;
    CGFloat label13Y=5;
    CGFloat label13W=75;
    CGFloat label13H=40;
    label13.frame=CGRectMake(label13X, label13Y, label13W, label13H);
    label13.text=@"菜品：";
    label13.font=[UIFont systemFontOfSize:25];
    label13.textColor=[UIColor greenColor];
    label13.backgroundColor=[UIColor whiteColor];
    lab13 = label13;
    
    UILabel *label14=[[UILabel alloc]init];
    CGFloat label14X=15;
    CGFloat label14Y=50;
    CGFloat label14W=290;
    CGFloat label14H=25;
    label14.frame=CGRectMake(label14X, label14Y, label14W, label14H);
    label14.text=@"";
    label14.font=[UIFont systemFontOfSize:15];
    label14.textColor=[UIColor blackColor];
    label14.backgroundColor=[UIColor whiteColor];
    lab14 = label14;
    
    UILabel *label15=[[UILabel alloc]init];
    CGFloat label15X=15;
    CGFloat label15Y=5;
    CGFloat label15W=60;
    CGFloat label15H=25;
    label15.frame=CGRectMake(label15X, label15Y, label15W, label15H);
    label15.text=@"";
    label15.font=[UIFont systemFontOfSize:15];
    label15.textColor=[UIColor blackColor];
    label15.backgroundColor=[UIColor whiteColor];
    lab15 = label15;
    
    UILabel *label16=[[UILabel alloc]init];
    CGFloat label16X=75;
    CGFloat label16Y=5;
    CGFloat label16W=100;
    CGFloat label16H=25;
    label16.frame=CGRectMake(label16X, label16Y, label16W, label16H);
    label16.text=@"";
    label16.font=[UIFont systemFontOfSize:15];
    label16.textColor=[UIColor blackColor];
    label16.backgroundColor=[UIColor whiteColor];
    lab16 = label16;
    
    UILabel *label17=[[UILabel alloc]init];
    CGFloat label17X=175;
    CGFloat label17Y=5;
    CGFloat label17W=130;
    CGFloat label17H=25;
    label17.frame=CGRectMake(label17X, label17Y, label17W, label17H);
    label17.text=@"";
    label17.font=[UIFont systemFontOfSize:15];
    label17.textColor=[UIColor blackColor];
    label17.backgroundColor=[UIColor whiteColor];
    lab17 = label17;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
